package jdbc;

import java.sql.*;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class JdbcUpdate2 {
    //修改课程信息,根据课程号其他信息
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url = "jdbc:mysql://localhost:3306/java76?useSSL=false";
        Connection connection = null;
        PreparedStatement st = null;
        try {
            connection = DriverManager.getConnection(url, "root", "root1234");
            System.out.println(connection.getClass().getName());
            Scanner input=new Scanner(System.in);
           System.out.println("请输入课程号：");
            long id=input.nextLong();
            System.out.println("请输入课程名：");
            input.nextLine();
            String  sname=input.nextLine();
            
            System.out.println("请输入课程学时：");
            int classhour=input.nextInt();
            System.out.println("请输入年级号：");
            long gid=input.nextLong();
            String sql="update s_subject set subjectname=? ,classhour=?,gradeid=? where id=?";
            System.out.println(sql);
            st = connection.prepareStatement(sql);
            //！！！给占位符赋值
            st.setString(1,sname);
            st.setInt(2,classhour);
            st.setLong(3,gid);
            st.setLong(4,id);
            int num = st.executeUpdate();
            System.out.printf("%d行被修改了",num);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                st.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
